package com.iknight.theory.controller;

import com.github.pagehelper.PageInfo;
import com.iknight.mgr.core.base.BaseController;
import com.iknight.mgr.core.constant.IkgConstantCode;
import com.iknight.mgr.core.utils.IkgStringUtils;
import com.iknight.mgr.core.utils.IkgWebUtils;
import com.iknight.theory.api.TPCurrentService;
import com.iknight.theory.api.TPTranPDeService;
import com.iknight.theory.api.TeTranService;
import com.iknight.theory.model.TETranModel;
import com.iknight.theory.model.TPCurrentModel;
import com.iknight.theory.model.TPTranPDeModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author JeryZym
 * @date 2017/12/20
 */
@RestController
@RequestMapping("/rest/tran")
@Api("配电变压器控制器")
public class TranController implements BaseController<TETranModel> {

    private final static Logger logger = LogManager.getLogger(TranController.class);

    @Autowired
    private TeTranService<TETranModel, String> teTranService;
    @Autowired
    private TPCurrentService<TPCurrentModel, String> tpCurrentService;
    @Autowired
    private TPTranPDeService<TPTranPDeModel,String> tpTranPDeService;

    /**
     * 格式化数据
     *
     * @param params
     * @return
     */
    @Override
    public TETranModel format(Map<String, Object> params) {
        TETranModel entity = new TETranModel();
        entity.setTran_id(IkgStringUtils.ObjectToString(params.get("tran_id")));
        if (IkgStringUtils.isNotNull(params.get("tran_no"))) {
            String tran_no = IkgStringUtils.ObjectToString(params.get("tran_no"));
            entity.setTran_no(tran_no);
        }
        if (IkgStringUtils.isNotNull(params.get("tran_name"))) {
            String tran_name = IkgStringUtils.ObjectToString(params.get("tran_name"));
            entity.setTran_name(tran_name);
        }
        if (IkgStringUtils.isNotNull(params.get("tran_capacity"))) {
            String value = IkgStringUtils.ObjectToString(params.get("tran_capacity"));
            entity.setTran_capacity(Double.valueOf(value.equals("") ? "0" : value));
        }
        if (IkgStringUtils.isNotNull(params.get("tran_model"))) {
            String tran_model = IkgStringUtils.ObjectToString(params.get("tran_model"));
            entity.setTran_model(tran_model);
        }
        if (IkgStringUtils.isNotNull(params.get("tg_id"))) {
            String tg_id = IkgStringUtils.ObjectToString(params.get("tg_id"));
            entity.setTg_id(tg_id);
        }
        if (IkgStringUtils.isNotNull(params.get("section_date"))) {
            String section_date = IkgStringUtils.ObjectToString(params.get("section_date"));
            entity.setSection_date(section_date);
        }
        if (IkgStringUtils.isNotNull(params.get("is_effective"))) {
            String is_effective = IkgStringUtils.ObjectToString(params.get("is_effective"));
            entity.setIs_effective(is_effective);
        }

        return entity;
    }

    @Override
    @PostMapping("/save")
    @ApiOperation(value = "保存方法", httpMethod = "POST")
    public Object save(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teTranService.save(format(params));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "保存成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/delete")
    @ApiOperation(value = "删除方法", httpMethod = "POST")
    public Object delete(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("tran_id"));
            teTranService.delete(pKey);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "删除成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/selectOne")
    @ApiOperation(value = "单条记录查询方法", httpMethod = "POST")
    public Object selectOne(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("tg_id"));
            TETranModel entity = teTranService.selectOne(pKey);
            return IkgWebUtils.postIkgResponse(true, "查询成功!", entity);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
    }

    @Override
    @PostMapping("/list")
    @ApiOperation(value = "根据参数分页查询", httpMethod = "POST")
    public Object selectByPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
//        return null;
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        PageInfo<TETranModel> pageInfo = teTranService.selectPage(params, pageIndex, pageSize);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @PostMapping("/getTranCurrentCurse")
    @ApiOperation(value = "获取电流曲线")
    public Object getTranCurrentCurse(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        String tranId = IkgStringUtils.ObjectToString(params.get("tran_id"));
        String recordId = IkgStringUtils.ObjectToString(params.get("record_id"));
        Map result = new HashMap();
        result.put("curse", tpCurrentService.getTranCurrentCurse(tranId));
        result.put("result",tpTranPDeService.getInfoByRecordId(recordId));

        return result;
    }

    /**
     * 判断主键是否存在
     *
     * @param params
     * @return
     */
    @Override
    public boolean isPKeyNotNull(Map<String, Object> params) {
        return null != params && IkgStringUtils.isNotNull(params.get("tran_id"));
    }
}
